#!/bin/bash
set -e
[% c("var/set_default_env") -%]
[% c("var/setarch") -%]
[% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]
distdir="/var/tmp/dist/[% project %]"
[% IF c("var/linux") -%]
  [% c("var/set_PTDIR_DOCSDIR") -%]
  mkdir -p $PTDIR
[% END -%]
mkdir -p /var/tmp/build
tar -C /var/tmp/build -xf $rootdir/[% project %]-[% c('version') %].tar.gz
cd /var/tmp/build/[% project %]-[% c('version') %]
[% IF c("var/linux-i686") -%]
  export CFLAGS=-m32
  export CXXFLAGS=-m32
  export LDFLAGS=-m32
[% END -%]
[% IF c("var/windows") -%]
  pydir="$distdir/python"
  mkdir -p "$pydir"
  export FAKETIME="[% USE date; GET date.format(c('timestamp'), format = '%Y-%m-%d %H:%M:%S') %]"
  export LD_PRELOAD=[% c("var/faketime_path") %]
  # This is bogus, that we run the configure script in the build environment,
  # but it seems to work. https://bugs.launchpad.net/pycrypto/+bug/1096207 for
  # ac_cv_func_malloc_0_nonnull.
  ac_cv_func_malloc_0_nonnull=yes sh configure --host=i686-w64-mingw32
  export LD_PRELOAD=
  $PYTHON setup.py build_ext -c mingw32
  $PYTHON setup.py install --prefix=$(wine winepath -w $pydir)
[% ELSE -%]
  export PYTHON=python2
  ./configure --build=i686-linux-gnu [% c("var/configure_opt") %]
  python2 setup.py build --build-lib build
  cp -a build/Crypto $PTDIR/
[% END -%]
cd $distdir
[% c('tar', {
        tar_src => [ '.' ],
        tar_args => '-czf ' _ dest_dir _ '/' _ c('filename'),
    }) %]
